<!DOCTYPE html>
<html lang="en">
<head>

<script>
    let loginList = [ 
  {userName: 'admin', password: 'admin321'},
  {userName: 'zhangsan', password: 'zs321'},
  {userName: 'lisi', password: 'ls321'},
  {userName: 'yyqx', password: 'yyqx321'}
]

// 现有的用户信息列表
// 根据生成的token 去掉找到 对应的信息
let userInfo = {
  token1: {
    city: '杭州',
    money: '1000w',
    sex: '女',
    userName: 'admin',
    password: 'admin321'
  },
  token2: {
    city: '北京',
    money: '1100w',
    sex: '女',
    userName: 'zhangsan',
    password: 'zs321'
  },
  token3: {
    city: '北京',
    money: '800w',
    sex: '男',
    userName: 'lisi', 
    password: 'ls321'
  },
  token4: {
    city: '天津',
    money: '600w',
    sex: '男',
    userName: 'yyqx',
    password: 'yyqx321'
  }
}

// 生成随机token
function generateToken() {
  const randomNum = Math.floor(Math.random() * 4) + 1;
  return 'token' + randomNum;
}

// 登录操作
function login() {
  const userName = prompt('请输入用户名');
  const password = prompt('请输入密码');
  // 验证登录信息
  const loginUser = loginList.find(item => item.userName === userName && item.password === password);
  if (loginUser) {
    const token = generateToken();
    const userDetail = userInfo[token];
    alert(`登录成功，欢迎 ${userDetail.userName}，您的详细信息为：\n城市：${userDetail.city}\n资产：${userDetail.money}\n性别：${userDetail.sex}`);
  } else {
    const action = prompt('登录失败，请选择操作：\n1. 注册\n2. 直接退出');
    if (action === '1') {
      const newUserName = prompt('请输入新用户名');
      const newPassword = prompt('请输入新密码');
      // 追加到用户列表
      loginList.push({userName: newUserName, password: newPassword});
      alert('注册成功，请重新登录');
      login();
    } else if (action === '2') {
      alert('无操作机会，退出');
    } else {
      alert('输入有误，退出');
    }
  }
}
</script>
</body>
</html>